package com.bckj.fastboot.core.extra.dbsearch.configuration;

import com.bckj.fastboot.core.extra.dbsearch.DbSearcher;
import com.bckj.fastboot.core.extra.dbsearch.dbmodel.DbModelInitialization;
import com.bckj.fastboot.core.extra.dbsearch.dbmodel.DbModelScanner;
import com.bckj.fastboot.core.extra.dbsearch.dbview.DbViewFactory;
import com.bckj.fastboot.core.extra.dbsearch.servlet.DbSearchServlet;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

@AutoConfiguration
@RequiredArgsConstructor
@ConditionalOnProperty(name = "fastboot.dbsearch.enabled", havingValue = "true")
@EnableConfigurationProperties(DbSearchProperties.class)
public class DbSearchConfiguration {

    private final DbSearchProperties dbSearchProperties;

    @Bean
    public DbSearcher dbSearcher() {
        return new DbSearcher();
    }

    @Bean
    public ServletRegistrationBean<DbSearchServlet> servletRegistrationBean(DbSearcher dbSearcher) {
        ServletRegistrationBean<DbSearchServlet> registrationBean = new ServletRegistrationBean<>(new DbSearchServlet(dbSearcher), "/search/*");
        // 可以设置其他属性，如初始化参数等
        return registrationBean;
    }

    @Bean
    public DbViewFactory dbViewFactory() {
        return new DbViewFactory();
    }

    @Bean
    public DbModelScanner dbModelScanner() {
        return new DbModelScanner();
    }

    @Bean
    public DbModelInitialization dbModelInitialization(DbModelScanner dbModelScanner) {
        return new DbModelInitialization(dbModelScanner, dbSearchProperties.getBasePackages());
    }
}
